{extend name="v1/layout/base"}
{block name="content"}
<div class="layuimini-main">

  <fieldset class="table-search-fieldset xk-search-status" id="xk-search-status">
    <legend>搜索信息</legend>
    <div style="margin: 10px 10px 10px 10px">
      <div class="layui-row layui-col-space15">
        <div class="layui-col-md6 xz_width">
          <form class="layui-form">
            <div class="layui-card-body layui-form layui-form-pane">
              <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg12">
                  <label class="layui-form-label">转出会员号</label>
                  <div class="layui-input-block">
                    <input type="text" name="push_user_sn" placeholder="转出会员号" id="push_user_sn" class="layui-input">
                  </div>
                </div>
              </div>
              <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg12">
                  <label class="layui-form-label">转入会员号</label>
                  <div class="layui-input-block">
                    <input type="text" name="user_sn" placeholder="转入会员号" id="user_sn" class="layui-input">
                  </div>
                </div>
              </div>
              <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg12">
                  <label class="layui-form-label">审核状态</label>
                  <div class="layui-input-block">
                    <input type="checkbox" class="layui-input" lay-skin="primary" name="status" value="0" title="未审核">
                    <input type="checkbox" class="layui-input" lay-skin="primary" name="status" value="1" title="审核成功">
                    <input type="checkbox" class="layui-input" lay-skin="primary" name="status" value="2" title="审核失败">
                  </div>
                </div>
              </div>
              <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg12">
                  <label class="layui-form-label">时间段</label>
                  <div class="layui-input-block">
                    <input type="text" class="layui-input" autocomplete="off" id="search_time" placeholder="请选择对应的时间范围">
                  </div>
                </div>
              </div>
              <div class="layui-row layui-col-space10 layui-form-item">
                <div class="layui-col-lg12">
                  <label class="layui-form-label">范围</label>
                  <div class="layui-input-inline" style="width: 100px;">
                    <input type="text" name="min_price" id="min_price" placeholder="￥" autocomplete="off" class="layui-input">
                  </div>
                  <div class="layui-form-mid">-</div>
                  <div class="layui-input-inline" style="width: 100px;">
                    <input type="text" name="max_price" id="max_price" placeholder="￥" autocomplete="off" class="layui-input">
                  </div>
                </div>
              </div>
              <div class="layui-row">
                <div class="layui-col-xs4">&nbsp;</div>
                <div class="layui-col-xs4">
                  <button type="reset" class="layui-btn layui-btn-primary  xk-refresh"><i class="fa fa-refresh"></i>&nbsp;重置</button>
                  <button type="button" class="layui-btn layui-btn-normal xk-dosearch">搜索<i class="layui-icon"></i></button>
                  <button type="button" class="layui-btn layui-btn-primary close-search"><i class="layui-icon">&#x1007;</i>关闭</button>
                </div>
                <div class="layui-col-xs4">&nbsp;</div>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </fieldset>

  <script type="text/html" id="toolbar">
    <div class="layui-btn-container">
      <button class="layui-btn layui-btn-black  layui-btn-sm" lay-event="reseve"><i class="layui-icon">&#xe63d;</i></button>
      <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="export"><i class="layui-icon">&#xe67d;</i>导出筛选后的数据</button>
      <div class="money_all"></div>
    </div>
  </script>

  <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
  <script type="text/html" id="currentTableBar">
    {{# if(d.status == '等待处理'){ }}
    <div class="layui-btn-group">
      <button class="layui-btn layui-btn-radius layui-btn-normal layui-btn-sm" lay-event="doAgentFinance">审核</button>
    </div>
    {{# } }}
  </script>
</div>
{/block}
{block name="myScript"}
<script>
  layui.use(['form', 'table', 'laydate'], function() {
    var $ = layui.jquery,
      form = layui.form,
      table = layui.table;
    var laydate = layui.laydate;
    laydate.render({
      elem: '#search_time',
      range: '~',
      theme: '#1E9FFF',
      type: 'datetime',
    });
    table.render({
      elem: '#currentTableId',
      url: '/{:config("manage.manage_name")}/{:config("manage.version")}/Finance/balanceTurn',
      toolbar: '#toolbar',
      defaultToolbar: ['filter', {
        title: '搜索',
        layEvent: 'search',
        icon: 'layui-icon-search'
      }],
      height: 'full-50',
      cols: [
        [{
            type: "checkbox"
          }, {
            field: 'push_user_sn',
            width: '10%',
            title: '转出会员号'
          },
          {
            field: 'push_user_name',
            width: '10%',
            title: '转出会员昵称'
          },
          {
            field: 'user_sn',
            width: '10%',
            title: '接收会员号'
          },
          {
            field: 'user_name',
            width: '10%',
            title: '接收会员昵称'
          },
          {
            field: 'money',
            width: '8%',
            title: '交易金额'
          },
          {
            field: 'chanage_money',
            width: '8%',
            title: '转出时余额'
          },
          {
            field: 'create_time',
            minWidth: '200',
            fixed: 'right',
            title: '创建时间'
          }
        ]
      ],
      done: function (res, curr, count) {
        $(".money_all").html(res.resultData.show_count);
      },
      limits: [100, 150, 200, 250, 500, 1000],
      limit: 100,
      id: 'table_ids',
      page: true,
      skin: 'line'
    });
    // 顶部操作工具
    table.on('toolbar(currentTableFilter)', function(obj) {
      switch (obj.event) {
        case 'reseve':
          table.reload('table_ids');
          break;
        case 'doAgentFinance':
          var push_user_sn = $("#push_user_sn").val(),
            user_sn = $("#user_sn").val(),
            search_time = $("#search_time").val(),
            max_price = $("#max_price").val(),
            min_price = $("#min_price").val(),
            status = '',
            status_arr = [];
          url = "/{:config('manage.manage_name')}/{:config('manage.version')}/Finance/doBalanceFinance";
          $("input[name=status]:checked").each(function() {
            status_arr.push($(this).val());
          });
          status = status_arr.join(',');
          var id_arr = getCheckValue();
          if (id_arr == '') {
            layer.msg("请选择需要审核的数据");
            return false;
          }
          //确定要删除吗？
          layer.msg('请选择针对选中数据的操作', {
            time: 0, //不自动关闭
            btn: ['审核通过', '审核不通过'],
            yes: function(index) {
              layer.close(index);
              https(url, {
                id_str: id_arr
              }, 'post', true, '正在执行通过转赠操作').then(res => {
                layer.msg(res.result_msg);
                if (res.result_code == 200) {
                  table.reload('table_ids', {
                    where: {
                      push_user_sn: push_user_sn,
                      user_sn: user_sn,
                      search_time: search_time,
                      max_price: max_price,
                      min_price: min_price,
                      status: status
                    }
                  });
                }
              }, error => {

              })
            },
            btn2: function(index) {
              layer.close(index);
              var html =
                '<div class="layui-col-md12"><div class="layui-card-body layui-form layui-form-pane"><div class="layui-row layui-col-space12 layui-form-item"><div class="layui-col-lg8"><label class="layui-form-label">请输入拒绝审核的理由：</label><div class="layui-input-block"><input type="text" id="why_content" autocomplete="off" value="" class="layui-input"></div></div></div>';
              //询问框
              var showx = layer.open({
                type: 1,
                title: '审核不通过',
                shadeClose: false,
                shade: false,
                maxmin: false,
                area: ['800px', '400px'],
                btn: ['确认', '取消'],
                content: html,
                yes: function(index, layero) {
                  var why_content = $("#why_content").val();
                  https(url, {
                    content: why_content,
                    id_str: id_arr,
                    status: 2
                  }, 'post', 1, '正在执行拒绝转赠操作').then(res => {
                    layer.msg(res.result_msg);
                    setTimeout(function() {
                      table.reload('table_ids', {
                        page: {
                          curr: 1
                        },
                        where: {
                          push_user_sn: push_user_sn,
                          user_sn: user_sn,
                          search_time: search_time,
                          max_price: max_price,
                          min_price: min_price,
                          status: status
                        }
                      }, 'data');
                      layer.close(showx);
                    }, 1500)
                  })
                },
                btn2: function() {
                  //alert("222");  取消时触发
                }
              });
            }
          });
          break;
        case 'export':
        var push_user_sn = $("#push_user_sn").val(),
          user_sn = $("#user_sn").val(),
          search_time = $("#search_time").val(),
          max_price = $("#max_price").val(),
          min_price = $("#min_price").val(),
          status = '',
          status_arr = [];
          $("input[name=status]:checked").each(function() {
            status_arr.push($(this).val());
          });
          status = status_arr.join(',');
          url = "/{:config('manage.manage_name')}/{:config('manage.version')}/Finance/exportBalanceTurn";
          https(url, {
            push_user_sn: push_user_sn,
            user_sn: user_sn,
            search_time: search_time,
            max_price: max_price,
            min_price: min_price,
            status: status
          }, 'post', true, '正在执行余额转赠数据导出').then(res => {
            if (res.result_code == 200) {
              var html = '<h1 style="padding: 0 20px;color:red;font-size:18px;margin-bottom:15px;">如果数据量大于1万条系统自动分表导出</h1>';
              res.resultData.forEach(function(value, index) {
                html += '<h2 style="padding: 0 20px;font-size:16px;">第' + (index + 1) + '个订单文件：<a href="' + value + '">' + '下载' + '</a></h2>';
              })
              layer.open({
                type: 1,
                skin: 'layui-layer-rim', //加上边框
                area: ['400px', '200px'], //宽高
                content: html
              });
            }
          })
          break;
        case 'search':
          $("#xk-search-status").removeClass('xk-search-status');
          break;
      }
    })
    // 列表工具栏目
    table.on('tool(currentTableFilter)', function(obj) {
      var data = obj.data,
        event = obj.event;
      switch (event) {
        case 'doAgentFinance':
          var push_user_sn = $("#push_user_sn").val(),
            user_sn = $("#user_sn").val(),
            search_time = $("#search_time").val(),
            max_price = $("#max_price").val(),
            min_price = $("#min_price").val(),
            status = '',
            status_arr = [];
          url = "/{:config('manage.manage_name')}/{:config('manage.version')}/Finance/doBalanceFinance";
          $("input[name=status]:checked").each(function() {
            status_arr.push($(this).val());
          });
          status = status_arr.join(',');
          //确定要删除吗？
          layer.msg('请选择针对用户' + data.push_user_sn + '转赠余额给用户' + data.user_sn + '的结果', {
            time: 0, //不自动关闭
            btn: ['审核通过', '审核不通过'],
            yes: function(index) {
              layer.close(index);
              https(url, {
                id_str: data.money_chanage_id
              }, 'post', true, '正在执行通过转赠操作').then(res => {
                layer.msg(res.result_msg);
                if (res.result_code == 200) {
                  table.reload('table_ids', {
                    where: {
                      push_user_sn: push_user_sn,
                      user_sn: user_sn,
                      search_time: search_time,
                      max_price: max_price,
                      min_price: min_price,
                      status: status
                    }
                  });
                }
              }, error => {

              })
            },
            btn2: function(index) {
              layer.close(index);
              var html =
                '<div class="layui-col-md12"><div class="layui-card-body layui-form layui-form-pane"><div class="layui-row layui-col-space12 layui-form-item"><div class="layui-col-lg8"><label class="layui-form-label">请输入拒绝审核的理由：</label><div class="layui-input-block"><input type="text" id="why_content" autocomplete="off" value="" class="layui-input"></div></div></div>';
              //询问框
              var showx = layer.open({
                type: 1,
                title: '审核不通过',
                shadeClose: false,
                shade: false,
                maxmin: false,
                area: ['800px', '400px'],
                btn: ['确认', '取消'],
                content: html,
                yes: function(index, layero) {
                  var why_content = $("#why_content").val();
                  https(url, {
                    content: why_content,
                    id_str: data.money_chanage_id,
                    status: 2
                  }, 'post', 1, '正在执行拒绝转赠操作').then(res => {
                    layer.msg(res.result_msg);
                    setTimeout(function() {
                      table.reload('table_ids', {
                        page: {
                          curr: 1
                        },
                        where: {
                          push_user_sn: push_user_sn,
                          user_sn: user_sn,
                          search_time: search_time,
                          max_price: max_price,
                          min_price: min_price,
                          status: status
                        }
                      }, 'data');
                      layer.close(showx);
                    }, 1500)
                  })
                },
                btn2: function() {
                  //alert("222");  取消时触发
                }
              });
            }
          });
          break;
        default:

      }

    })
    // 执行搜索
    $(".xk-dosearch").click(function() {
      var push_user_sn = $("#push_user_sn").val(),
        user_sn = $("#user_sn").val(),
        search_time = $("#search_time").val(),
        max_price = $("#max_price").val(),
        min_price = $("#min_price").val(),
        status = '',
        status_arr = [];
      $("input[name=status]:checked").each(function() {
        status_arr.push($(this).val());
      });
      status = status_arr.join(',');
      table.reload('table_ids', {
        page: {
          curr: 1
        },
        where: {
          push_user_sn: push_user_sn,
          user_sn: user_sn,
          search_time: search_time,
          max_price: max_price,
          min_price: min_price,
          status: status
        }
      }, 'data');
    })
    // 获取选中项
    function getCheckValue() {
      var id_arr = [],
        id_str = '';
      var checkStatus = table.checkStatus('table_ids'),
        data = checkStatus.data;
      data.map(function(d) {
        id_arr.push(d.money_chanage_id);
      })
      id_str = id_arr.join(',');
      return id_str;
    }
  });
</script>
{/block}
